var app = getApp(), api = getApp().api;

Page({
    data: {
        isPageShow: !1,
        order: null,
        getGoodsTotalPrice: function() {
            return this.data.order.total_price;
        }
    },
    onLoad: function(e) {
        getApp().page.onLoad(this, e);
        var t = this;
        getApp().core.showLoading({
            title: "正在加载"
        });
        var o = getCurrentPages(), a = o[o.length - 2];
        getApp().request({
            url: getApp().api.order.detail,
            data: {
                order_id: e.id,
                route: a.route
            },
            success: function(e) {
                0 == e.code && t.setData({
                    order: e.data,
                    isPageShow: !0
                });
            },
            complete: function() {
                getApp().core.hideLoading();
            }
        });
    },
    copyText: function(e) {
        var t = e.currentTarget.dataset.text;
        getApp().core.setClipboardData({
            data: t,
            success: function() {
                getApp().core.showToast({
                    title: "已复制"
                });
            }
        });
    },
    location: function() {
        var e = this.data.order.shop;
        getApp().core.openLocation({
            latitude: parseFloat(e.latitude),
            longitude: parseFloat(e.longitude),
            address: e.address,
            name: e.name
        });
    },
  // 点击退款
  orderRevoke: function (t) {
    var a = this;

    getApp().core.showLoading({
      title: "正在加载",
      mask: !0
    }),

      // 读取数据库设置的退款时间

      getApp().request({
        url: getApp().api.order.system_msg,
        data: {
          id: '',
        },
        success: function (system_msg) {
          a.system_msg = system_msg,
            0 == system_msg.code && system_msg.setData({
              system_msg: system_msg,
            });

          console.log("后台系统设置信息：")
          console.log(system_msg)
        },
      });


    getApp().request({
      url: getApp().api.order.detail,
      data: {
        order_id: t.currentTarget.dataset.id,
      },
      success: function (e) {
        0 == e.code && a.setData({
          order_detail: e.data,
        });
        getApp().core.hideLoading();

        console.log("数据库下单时间：" + e.data.addtime);
        console.log("系统设置的退款时间限制为：" + a.system_msg[0].refund_time + '天')



        // 数据库下单时间
        var addtime = e.data.addtime;

        var format = addtime.replace(/-/g, '/')
        var date = Date.parse(new Date(format))

        var date = new Date(date);
        var Y = date.getFullYear() + '/';
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/';
        var D = date.getDate() + parseInt(a.system_msg[0].refund_time) + ' ';
        var h = date.getHours() + ':';
        var m = date.getMinutes() + ':';
        var s = date.getSeconds();
        var endtime = Y + M + D + h + m + s;
        console.log("可申请退款的时间：" + endtime);

        var date2 = new Date(endtime);    //结束时间

        var curtime = new Date().format("yyyy-MM-dd hh:mm:ss");
        var curtime = curtime.replace(/-/g, '/')
        console.log("当前的时间：" + curtime);


        var date3 = date2.getTime() - new Date();   //时间差的毫秒数           

        //------------------------------
        var subMinutes = Math.floor(date3 / (60 * 1000)) //获取总共的分钟差



        //计算出相差天数
        var days = Math.floor(date3 / (24 * 3600 * 1000))

        //计算出小时数

        var leave1 = date3 % (24 * 3600 * 1000)    //计算天数后剩余的毫秒数
        var hours = Math.floor(leave1 / (3600 * 1000))
        //计算相差分钟数
        var leave2 = leave1 % (3600 * 1000)        //计算小时数后剩余的毫秒数
        var minutes = Math.floor(leave2 / (60 * 1000))
        //计算相差秒数
        var leave3 = leave2 % (60 * 1000)      //计算分钟数后剩余的毫秒数
        var seconds = Math.round(leave3 / 1000)
        console.log(" 相差 " + days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒")

        // 时间相减 e

        if (seconds < 0) {
          console.log("可以退款！");
          var o = this;
          getApp().core.showModal({
            title: "提示",
            content: "是否退款该订单？",
            cancelText: "否",
            confirmText: "是",
            success: function (e) {
              if (e.cancel) return !0;
              e.confirm && (getApp().core.showLoading({
                title: "操作中"
              }), getApp().request({
                url: getApp().api.order.revoke,
                data: {
                  order_id: t.currentTarget.dataset.id
                },
                success: function (e) {
                  getApp().core.hideLoading(),
                    getApp().core.showModal({
                      title: "提示",
                      content: e.msg,
                      showCancel: !1,
                      success: function (o) {
                        e.confirm && e.onLoad({
                          id: o.data.order.order_id
                        });
                      }
                    });
                }
              }));
            }
          });
        } else {
          wx.showModal({
            title: '提示',
            content: " 请 " + days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒 后再来申请退款",
            showCancel: false,
            confirmText: "ok"
          })
        }

      },
      complete: function () {
        getApp().core.hideLoading();
      }
    });

  },
  
});